﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace PVP
{
    public partial class GRNForm : Form
    {
        private CPIII cp3;
        String[] files;
        string cp3path = Application.StartupPath + "\\" + "CPIII.csv";
        private lookup lkp = null;
        public int leftCurrent = -1;
        public int rightCurrent = -1;

        public GRNForm()
        {
            InitializeComponent();
        }

        private void button_ImportData_Click(object sender, EventArgs e)
        {
            DateTime olddt = DateTime.Now;
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            folderBrowserDialog1.ShowNewFolderButton = false;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            }
            else
            {
                return;
            }
        }

        private void button_filterfile_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            folderBrowserDialog1.ShowNewFolderButton = false;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            }
            else
            {
                return;
            }
            new FilterFile(files, Application.StartupPath);
            new CheckFile(Application.StartupPath);
            new CoordinatesConversion();

        }


        private void button_processNA2_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = @"d:\";
            openFileDialog1.Filter = "GSI files (*.GSI)|*.GSI|NA2 files (*.NA2)|*.NA2|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.FileName = "*.*";
        }

        private void button_ImportCPIII_Click(object sender, EventArgs e)
        {

            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;
            if (File.Exists(cp3path))
            {
                result = MessageBox.Show("程序中已有CPIII控制文件，是否重新导入？", "提示", buttons);
                if (result == DialogResult.Yes)
                {
                    folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
                    folderBrowserDialog1.ShowNewFolderButton = false;
                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {
                        files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                    }
                    cp3 = new CPIII(files);
                    using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\" + "CPIII.csv"))
                    {
                        sw.Write(cp3.filetext);
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
                folderBrowserDialog1.ShowNewFolderButton = false;
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                }
                cp3 = new CPIII(files);
                using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\" + "CPIII.csv"))
                {
                    sw.Write(cp3.filetext);
                }
            }

        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button_left_grp_next_Click_1(object sender, EventArgs e)
        {
            leftCurrent++;
            GRP_move("left", leftCurrent);
        }
        private void button_left_grp_up_Click_1(object sender, EventArgs e)
        {
            leftCurrent--;
            GRP_move("left", leftCurrent);
        }

        private void button_right_grp_up_Click(object sender, EventArgs e)
        {
            rightCurrent--;
            GRP_move("right", rightCurrent);
        }

        private void button_right_grp_next_Click(object sender, EventArgs e)
        {
            rightCurrent++;
            GRP_move("right", rightCurrent);
        }

        private void GRP_move(string kind, int current)
        {
            if (lkp == null)
            {
                lkp = new lookup(Application.StartupPath);
            }
            if (kind == "left")
            {
                dataGridView_leftGRP.Rows.Clear();
                string[] strs = lkp.Next(kind, current);
                string[] stemp = null;
                if (strs == null) return;
                for (int i = 0; i < strs.Length; i++)
                {
                    stemp = strs[i].Split(',');
                    dataGridView_leftGRP.Rows.Add(stemp);
                }
            }
            if (kind == "right")
            {
                dataGridView_rightGRP.Rows.Clear();
                string[] strs = lkp.Next(kind, current);
                string[] stemp = null;
                if (strs == null) return;
                for (int i = 0; i < strs.Length; i++)
                {
                    stemp = strs[i].Split(',');
                    dataGridView_rightGRP.Rows.Add(stemp);
                }
            }

        }

        private void CheckRowsTolerance(DataGridView dgv, DataGridViewRowsAddedEventArgs e)
        {
            DataGridViewRow currentRow = dgv.Rows[e.RowIndex];
            List<Point2D> ptlist = new List<Point2D>();
            for (int i = 1; i < currentRow.Cells.Count; )
            {
                if (currentRow.Cells[i].Value == null)
                {
                    i++;
                    continue;
                }
                else
                {
                    ptlist.Add(new Point2D(double.Parse((string)currentRow.Cells[i].Value), double.Parse((string)currentRow.Cells[i + 1].Value.ToString())));
                    i += 2;
                }
            }
            double xiancha = double.Parse(numericUpDown_Restrict.Value.ToString()) / 1000;
            Point2D ave = myMath.ave(ptlist);

            foreach (Point2D pt in ptlist)
            {
                if ((Math.Abs(pt.X - ave.X) > xiancha) || (Math.Abs(pt.Y - ave.Y) > xiancha))
                {
                    dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                }
            }
        }

        private void dataGridView_leftGRP_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            CheckRowsTolerance(dataGridView_leftGRP, e);
        }

        private void dataGridView_rightGRP_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            CheckRowsTolerance(dataGridView_rightGRP, e);
        }

        

       





    }
}